package com.ity.teamanagement.service.Impl;

import com.ity.teamanagement.code.CommonCode;
import com.ity.teamanagement.code.ImgCode;
import com.ity.teamanagement.entity.OtherTeaImg;
import com.ity.teamanagement.entity.TeaImg;
import com.ity.teamanagement.exception.ExceptionUtils;
import com.ity.teamanagement.mapper.TeaImgMapper;
import com.ity.teamanagement.service.FileService;
import com.ity.teamanagement.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author coderFu
 * @create 2021/6/2
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {

    @Resource
    private TeaImgMapper teaImgMapper;


    @Override
    public TeaImg saveImg(MultipartFile multipartFile, Long imgType, Long keyId) {
        if (multipartFile ==null || multipartFile.isEmpty()){
            ExceptionUtils.error(ImgCode.IMG_NOT_EXIST);
        }
        if (!FileUtils.checkImgSize(multipartFile)){
            ExceptionUtils.error(ImgCode.IMG_SIZE_BEYOND);
        }
        String filePath = "";
        try{
            //文件上传
            filePath = FileUtils.uploadFile(multipartFile);
        }catch (Exception e){
           ExceptionUtils.error(ImgCode.IMG_UPDATE_ERROR);
        }
        TeaImg teaImg = new TeaImg();
        teaImg.setPath(filePath);
        teaImg.setCreateTime(new Date());
        teaImg.setImgType(imgType);
        //keyId是否存在
        if (keyId!=null){
            teaImg.setKeyId(keyId);
            Integer count = teaImgMapper.saveImg(teaImg);
            if (count<1){
                ExceptionUtils.error(ImgCode.IMG_UPDATE_ERROR);
            }
            return teaImg;
        }
        Long aLong = teaImgMapper.findMaxId();
        if (aLong == null){
            aLong = 0L;
        }
        teaImg.setKeyId(aLong);
        Integer count = teaImgMapper.saveImg(teaImg);
        if (count<1){
            ExceptionUtils.error(ImgCode.IMG_UPDATE_ERROR);
        }
        return teaImg;
    }

    @Override
    public TeaImg deleteTeaImg(Long id) {
        if (id == null){
            ExceptionUtils.error(CommonCode.PARAMETER_ERROR);
        }
        TeaImg teaImg = teaImgMapper.findImgById(id);
        if (teaImg == null){
            ExceptionUtils.error(ImgCode.IMG_NOT_EXIST);
        }
        boolean flag = false;
        try{
            flag = FileUtils.deleteFile(teaImg.getPath());
            if (!flag){
                log.info(teaImg.getPath()+"图片删除失败");
            }
        }catch (Exception e){
            log.info(teaImg.getPath()+"图片删除失败");
        }
        Integer count = teaImgMapper.deleteImgById(id);
        if (count<1){
            ExceptionUtils.error(ImgCode.IMG_DELETE_ERROR);
        }
        return teaImg;
    }

    @Override
    public OtherTeaImg uploadOtherImg(MultipartFile multipartFile, Long keyId) {
        if (multipartFile ==null || multipartFile.isEmpty()){
            ExceptionUtils.error(ImgCode.IMG_NOT_EXIST);
        }
        if (!FileUtils.checkImgSize(multipartFile)){
            ExceptionUtils.error(ImgCode.IMG_SIZE_BEYOND);
        }
        String filePath = "";
        try{
            //文件上传
            filePath = FileUtils.uploadFile(multipartFile);
        }catch (Exception e){
            ExceptionUtils.error(ImgCode.IMG_UPDATE_ERROR);
        }
        OtherTeaImg otherTeaImg = new OtherTeaImg();
        otherTeaImg.setPath(filePath);
        otherTeaImg.setCreateTime(new Date());
        if (keyId != null){
            otherTeaImg.setOtherKeyId(keyId);
            Integer count = teaImgMapper.saveOtherImg(otherTeaImg);
            if (count<1){
                ExceptionUtils.error(ImgCode.IMG_UPDATE_ERROR);
            }
            return otherTeaImg;
        }
        Long aLong = teaImgMapper.findOtherImgMaxId();
        if (aLong == null){
            aLong = 0L;
        }
        otherTeaImg.setOtherKeyId(aLong);
        Integer count = teaImgMapper.saveOtherImg(otherTeaImg);
        if (count<1){
            ExceptionUtils.error(ImgCode.IMG_UPDATE_ERROR);
        }
        return otherTeaImg;
    }

    @Override
    public OtherTeaImg deleteOtherImg(Long id) {
        if (id == null){
            ExceptionUtils.error(CommonCode.PARAMETER_ERROR);
        }
        OtherTeaImg otherTeaImg = teaImgMapper.findOtherImgById(id);
        if (otherTeaImg == null){
            ExceptionUtils.error(ImgCode.IMG_NOT_EXIST);
        }
        boolean flag = false;
        try{
            flag = FileUtils.deleteFile(otherTeaImg.getPath());
            if (!flag){
                log.info(otherTeaImg.getPath()+"图片删除失败");
            }
        }catch (Exception e){
            log.info(otherTeaImg.getPath()+"图片删除失败");
        }
        Integer count = teaImgMapper.deleteOtherImgById(id);
        if (count<1){
            ExceptionUtils.error(ImgCode.IMG_DELETE_ERROR);
        }
        return otherTeaImg;
    }
}
